/**
 * 执行验证公式
 * @param ds 待验证的ds
 * @param fieldArr 需要验证的ds属性，为null则遍历ds
 * @return 如果不通过，返回true
 */
function executeWebbxValidateFormular(ds){
	var meta = ds.metadata; 
	/*构造验证公式*/
	var	vfListStr = "{javaClass:'java.util.ArrayList',list:[";
	var vfArr = new Array();
	var flag1 = false;
	for(var i=0,len=meta.length; i<len; i++){
		var f = meta[i].validateFormular;
		if( f != null && f != ""){
			vfArr.push(f);
			flag1 = true;
		}
	}
	if(!flag1)
		return false;
	for(var i=0,len=vfArr.length;i<len;i++){
		vfListStr += "'" + vfArr[i] +"'";
		if(i != len-1)
			vfListStr += ",";
	}
	
	vfListStr += "]}";
	/*构造值*/
	var rows = ds.getRows();
	if(rows == null || ds.getRowCount() == 0)
		return false;
	var listStr = "{javaClass:'java.util.ArrayList',list:[";
	for(var k=0,len=rows.length; k<len; k++){
		var row = rows[k];
		var obj = new Object;
		obj.javaClass = 'java.util.HashMap';
		var objMap = new Object;
		obj.map = objMap;
		var objMap = "{javaClass:'java.util.HashMap',map:{";
		var arr = new Array();
		
		for(var i = 0,mlen=ds.metadata.length; i < mlen; i ++){
			var md = ds.metadata[i];
			if(md.field == null)
				continue;
			var value = row.getCellValue(i);
			//将"."替换成 _$_
			var replaceField = md.field.replace(".", "_$_");
			objMap[replaceField] = value;
			arr.push(replaceField +":'"+ value+"'");
		}
		
		objMap += arr.join(",");
		listStr += objMap + "}}";
		if( k != len-1)
			listStr += ",";
	}
	listStr += "]}";
	var dtMap = createDsDataTypeMap(ds);
	var result = getFormularService().executeVatelitionFormular(vfListStr, listStr,dtMap);
	return $executeResult11(result,ds,event);
};

/**
 * 编辑公式中的验证
 * @param result
 * @param ds
 * @param event
 * @return
 */
function $executeResult11(result,ds,event){
	var flag = false; 
	if(result == null) 
		return flag;
	var fv = result.map["formular_value"];
	if(fv == null || fv == '' || fv == '$NULL$')
		return flag;
	var fk = result.map["formular_key"];
	switch(fk){
		case "$Confirm":
			var params = [ds,event];
			$ok4ConfirmDlg.param = params;
			$cancen4ConfirmDlg.param = params;
			flag = true;
			showConfirmDialog(fv, $ok4ConfirmDlg, $cancen4ConfirmDlg);
			break;
		case "$Error":
			flag = true;
			showErrorDialog(fv);
			break;
		case "$Message":
			flag = false;
			showMessageDialog(fv);
			break;
		default :
			break;
	}
	return flag;
}
function $ok4ConfirmDlg(){ 
	
};
function $cancen4ConfirmDlg(){
	var params = $cancen4ConfirmDlg.param;
	var ds = params[0];
	var event = params[1];
	ds.setValueAt(event.cellRowIndex, event.cellColIndex, 0);
	delete($cancen4ConfirmDlg.param);
};

function createDsDataTypeMap(ds){
	var obj = new Object;
	obj.javaClass = 'java.util.HashMap';
	var objMap = new Object;
	obj.map = objMap;
	for(var i = 0,len=ds.metadata.length; i < len; i ++){
		var md = ds.metadata[i];
		if(md.field == null)
			continue;
		var replaceField = md.field.replace(".", "_$_");
		objMap[replaceField] = md.dataType;
	}
	return obj;
};